import { resolve, join } from 'path'
import FileUtil from './src/utils/FileUtil'

const apiUrl = resolve('E:/repo/test/kami/api')
const tname = 'ssly'
const gname = 'kami'

const init = async () => {
    const renamedir = [] as string[]
    await FileUtil.readDirectory(apiUrl, async (filepath: string) => {
        if (filepath.endsWith('pom.xml') && filepath.indexOf('target\\classes') == -1) {
            console.log(filepath)

            const dirname = filepath.substring(0, filepath.lastIndexOf('\\'))
            const filename = dirname.substring(dirname.lastIndexOf('\\') + 1)
            if (filename.startsWith(tname)) {
                renamedir.push(dirname)
            }
            try {
                let fstr = await FileUtil.readFile(filepath)
                if (fstr.indexOf(tname) != -1) {
                    fstr = fstr.replaceAll(tname, gname)
                    await FileUtil.writeFile(filepath, fstr)
                }
            } catch {
                console.log('处理失败', filename)
            }
        }
    })
    for (let i = 0; i < renamedir.length; i++) {
        const dirname = renamedir[i]
        const endindex = dirname.lastIndexOf('\\') + 1
        let filename = dirname.substring(endindex)
        filename = filename.replaceAll(tname, gname)
        const rename = dirname.substring(0, endindex) + filename
        FileUtil.renameFileOrDirectory(dirname, rename)
    }
}

init()
